package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.ExpandEditAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.OrderRecBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditLabelActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.expand_listview)
    ExpandableListView expandListview;
    private List<OrderRecBean.BodyBean> groupList = new ArrayList<>();
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private ExpandEditAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_label);
        ButterKnife.bind(this);
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getDatas();
    }

    private void initView() {
        OrderRecBean parentBean=getIntent().getParcelableExtra("bean");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("添加/修改工作类型");
        tvMiddleTitle.setVisibility(View.VISIBLE);
        initExpandAdapter(parentBean);
    }
    private void initExpandAdapter( OrderRecBean parentBean){
        mAdapter = new ExpandEditAdapter(new ArrayList<OrderRecBean.BodyBean>(), this);
        expandListview.setGroupIndicator(null);
        expandListview.setAdapter(mAdapter);
        expandListview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                if(groupPosition==(mAdapter.getGroupCount()-1)){
                    Intent i=new Intent(EditLabelActivity.this,EditLabelDetailActivity.class);
                    i.putExtra("flag", Constant.TWO);
                    i.putExtra("id",((OrderRecBean.BodyBean)mAdapter.getGroup(groupPosition)).getLabelid()+"");
                    i.putExtra("type",2+"");
                    startActivity(i);
                    return true;
                }else{
                    return false;
                }
            }
        });
        int width = getWindowManager().getDefaultDisplay().getWidth();
        expandListview.setIndicatorBounds(width - 100, width - 10);
        setDatas(new ArrayList<OrderRecBean.BodyBean>());
        mAdapter.setCallBack(new ExpandEditAdapter.CallBack() {
            @Override
            public void delete(int groupPosition, int childPosition,int labelId) {
                deleteLabel(labelId);
            }
            @Override
            public void edit(int position) {}
        });
        mAdapter.notifyDataSetChanged();
    }
//    重新组装数据,给组子条目加上一个空数据，来在布局加上一个添加按钮
    private List<OrderRecBean.BodyBean> reComebineDatas(OrderRecBean parentBean){
        OrderRecBean.BodyBean.ChildBeanX childBeanX=new OrderRecBean.BodyBean.ChildBeanX();
        childBeanX.setLabelname("");
        if(parentBean==null){
            return new ArrayList<OrderRecBean.BodyBean>();
        }
        List<OrderRecBean.BodyBean> bodyBeans = parentBean.getBody();
        if(bodyBeans!=null){

            List<OrderRecBean.BodyBean.ChildBeanX> childxs = new ArrayList<>();
//        获取行业用人二级
//            int position=0;
//            for (int i = 0; i < bodyBeans.size(); i++) {
//                if(15==bodyBeans.get(i).getLabelclassid()){
//                    childxs=bodyBeans.get(i).getChild();
//                    position=i;
//                };
//            }
//        行业用人三级装入二级
//            List<OrderRecBean.BodyBean.ChildBeanX> childBeanX1_uses=new ArrayList<>();
//            for (int i = 0; i < childxs.size(); i++) {
//                OrderRecBean.BodyBean.ChildBeanX childxs_item = childxs.get(i);
//                if(childxs_item!=null){
//                    List<OrderRecBean.BodyBean.ChildBeanX.ChildBean> child_items = childxs_item.getChild();
//                    if(child_items!=null){
//                        if(!child_items.isEmpty()){
//                            for (int i1 = 0; i1 < child_items.size(); i1++) {
//                                OrderRecBean.BodyBean.ChildBeanX childBeanX1=new OrderRecBean.BodyBean.ChildBeanX();
//                                childBeanX1.setLabelid(child_items.get(i1).getLabelid());
//                                childBeanX1.setLabeltype(child_items.get(i1).getLabeltype());
//                                childBeanX1.setLabelname(child_items.get(i1).getLabelname());
//                                LogUtils.d("setClassname:"+child_items.get(i1).getLabelname());
//                                childBeanX1_uses.add(childBeanX1);
//                            }
//                        }
//                    }
//                }
//            }
//            bodyBeans.get(position).getChild().clear();
//            LogUtils.d("childBeanX1_uses,size:"+childBeanX1_uses.size());
//            bodyBeans.get(position).setChild(childBeanX1_uses);

            int size=bodyBeans.size();
            for(int i=0;i<size;i++){
                OrderRecBean.BodyBean bodyBean = bodyBeans.get(i);
                List<OrderRecBean.BodyBean.ChildBeanX> newChildBeans=new ArrayList<>();
                List<OrderRecBean.BodyBean.ChildBeanX> oldChildBeans = bodyBean.getChild();
                if(size!=(i+1)){
                    if(oldChildBeans==null){
                        newChildBeans.add(childBeanX);
                    }else{
                        newChildBeans.addAll(oldChildBeans);
                        newChildBeans.add(childBeanX);
                    }
                }
                bodyBean.setChild(newChildBeans);
            }


        }

        return bodyBeans;
    }

    private void deleteLabel(int labelId) {
        loadingDialog.show();
        OkgoUtil.get(Http.DelMyIntent, EditLabelActivity.this)
                .params("labelid",labelId+"")
                .params("labeltype","1")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(EditLabelActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if(parentBean.getCode()== Constant.SUCESS){
                            getDatas();
                        }
                        ToastUtils.toastForShort(EditLabelActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(EditLabelActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(List<OrderRecBean.BodyBean> body) {
        if(body!=null){
            groupList.addAll(body);
            mAdapter.notifyDataSetChanged();


        }

    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.get(Http.MyIntentList, EditLabelActivity.this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        OrderRecBean parentBean = DealResult.getInstance().dealDatas(EditLabelActivity.this, response, new TypeToken<OrderRecBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            mAdapter.setDataList(reComebineDatas(parentBean));
                            int groupCount=expandListview.getCount();
                            if(groupCount>0){
                                for (int i = 0; i < groupCount; i++) {
                                    expandListview.expandGroup(i);
                                }
                            }
                        } else {
                            ToastUtils.toastForShort(EditLabelActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(EditLabelActivity.this, getString(R.string.bad_net));
                        loadingDialog.dismiss();
                    }
                });
    }

    @OnClick({R.id.tv_confirm,R.id.title_left_one_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
                startActivity(new Intent(this,CustomLabelActivity.class));
                break;
        }
    }
}
